
package coupons

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/coupons"
    couponsReq "github.com/flipped-aurora/gin-vue-admin/server/model/coupons/request"
)

type PointsCouponsService struct {}
// CreatePointsCoupons 创建pointsCoupons表记录
// Author [yourname](https://github.com/yourname)
func (pointsCouponsService *PointsCouponsService) CreatePointsCoupons(pointsCoupons *coupons.PointsCoupons) (err error) {
	err = global.GVA_DB.Create(pointsCoupons).Error
	return err
}

// DeletePointsCoupons 删除pointsCoupons表记录
// Author [yourname](https://github.com/yourname)
func (pointsCouponsService *PointsCouponsService)DeletePointsCoupons(id string) (err error) {
	err = global.GVA_DB.Delete(&coupons.PointsCoupons{},"id = ?",id).Error
	return err
}

// DeletePointsCouponsByIds 批量删除pointsCoupons表记录
// Author [yourname](https://github.com/yourname)
func (pointsCouponsService *PointsCouponsService)DeletePointsCouponsByIds(ids []string) (err error) {
	err = global.GVA_DB.Delete(&[]coupons.PointsCoupons{},"id in ?",ids).Error
	return err
}

// UpdatePointsCoupons 更新pointsCoupons表记录
// Author [yourname](https://github.com/yourname)
func (pointsCouponsService *PointsCouponsService)UpdatePointsCoupons(pointsCoupons coupons.PointsCoupons) (err error) {
	err = global.GVA_DB.Model(&coupons.PointsCoupons{}).Where("id = ?",pointsCoupons.Id).Updates(&pointsCoupons).Error
	return err
}

// GetPointsCoupons 根据id获取pointsCoupons表记录
// Author [yourname](https://github.com/yourname)
func (pointsCouponsService *PointsCouponsService)GetPointsCoupons(id string) (pointsCoupons coupons.PointsCoupons, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&pointsCoupons).Error
	return
}
// GetPointsCouponsInfoList 分页获取pointsCoupons表记录
// Author [yourname](https://github.com/yourname)
func (pointsCouponsService *PointsCouponsService)GetPointsCouponsInfoList(info couponsReq.PointsCouponsSearch) (list []coupons.PointsCoupons, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&coupons.PointsCoupons{})
    var pointsCouponss []coupons.PointsCoupons
    // 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&pointsCouponss).Error
	return  pointsCouponss, total, err
}
func (pointsCouponsService *PointsCouponsService)GetPointsCouponsPublic() {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
